package in.muton.edushare.components.calendar;

import in.muton.edushare.components.calendar.api.EventHelper;
import in.muton.edushare.components.calendar.api.PeopleManager;
import in.muton.edushare.pages.common.EdusharePanel;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.vo.Messages;
import com.edushare.common.vo.ScheduleNotifications;
import com.edushare.common.vo.Schedules;

public class Event extends EdusharePanel {

	public Event(String id, IModel<Schedules> model, Date currentDate) {
		super(id, model);

		this.schEvent = model;
		this.currentDate = currentDate;
		Schedules event = model.getObject();
		setDefaultModel(new CompoundPropertyModel<Schedules>(model.getObject()));
		Label l;
		add(l = new Label("eventTooltip", new PropertyModel<String>(this,
				"shortTitle")));
		if (EventHelper.isEditable(event, getLoggedInUser()))
			l.add(new SimpleAttributeModifier("class",
							"eventTooltip editable"));
		else
			l.add(new SimpleAttributeModifier("class",
					"eventTooltip noneditable"));
		add(new Label("schTitle"));
		add(new Label("currentStartDate", new PropertyModel<String>(this,
				"currentStartDate")));
		add(new Label("currentEndDate", new PropertyModel<String>(this,
				"currentEndDate")));
		add(new Label("schLocation"));
		add(new Label("schDesc"));
		add(new Label("creationTimestamp"));
		add(new Label("creatingUser", model.getObject().getCreatingUser()
				.getCompleteUserName()));
		add(new Label("updatedTimestamp"));
		add(new Label("repetitionLabel", getRepetitionString(event)));

		WebMarkupContainer wmcUser = new WebMarkupContainer("userEvent");
		add(wmcUser);
		wmcUser
				.setVisible(schEvent.getObject().getCreatorType() == Messages.RECEIVER_TYPE_USER);
		wmcUser.add(new Label("number", ""
				+ schEvent.getObject().getNotifiedUsers().size()));
		wmcUser.add(new Label("names", PeopleManager.getProperList(schEvent
				.getObject(), 15, 0)));
		wmcUser.add(new Label("acceptNum", ""
				+ PeopleManager.getInviteeCount(event, ScheduleNotifications.OPEN)));
		wmcUser.add(new Label("acceptNames", PeopleManager.getProperList(schEvent
				.getObject(), 15, ScheduleNotifications.OPEN)));
		

		WebMarkupContainer wmc = new WebMarkupContainer("hiddenData");
		wmc.add(new Label("schId"));
		wmc.add(new Label("schDesc"));
		wmc.add(new Label("schEnd", new SimpleDateFormat("d MMM yyyy--hh:mm a")
				.format(model.getObject().getSchEnd())));
		wmc.add(new Label("schLocation"));
		wmc.add(new Label("schStart", new SimpleDateFormat(
						"d MMM yyyy--hh:mm a").format(model.getObject()
						.getSchStart())));
		wmc.add(new Label("schTitle"));
		wmc.add(new Label("schType"));
		wmc.add(new Label("frequency"));
		wmc.add(new Label("repetitionEnd", new SimpleDateFormat("d MMM yyyy")
				.format(model.getObject().getRepetitionEnd())));
		
		wmc.add(new Label("schPPL", PeopleManager.getEmailList(event))
		.setVisible(schEvent.getObject().getCreatorType() == Messages.RECEIVER_TYPE_USER));
		add(wmc);
	}
	
	public String getFrequency(){
		switch(schEvent.getObject().getFrequency()){
		case Schedules.EVENT_DAILY:
			return "1";
		case Schedules.EVENT_MONTHLY:
			return "3";
		case Schedules.EVENT_ONCE_ONLY:
			return "0";
		case Schedules.EVENT_WEEKLY:
			return "2";
		default :
			return "4";
		}
	}

	public String getCurrentEndDate() {
		return new SimpleDateFormat("d MMM yyyy hh:mm a").format(EventHelper
				.getCurrentEndDate(currentDate, schEvent.getObject()));
	}

	public String getCurrentStartDate() {
		return new SimpleDateFormat("d MMM yyyy hh:mm a").format(EventHelper
				.getCurrentStartDate(currentDate, schEvent.getObject()));
	}

	public static String getRepetitionString(Schedules event2) {
		if (event2.getFrequency() != Schedules.EVENT_ONCE_ONLY) {
			String retString = "Occurs ";
			switch (event2.getFrequency()) {
			case (Schedules.EVENT_DAILY):
				retString += "every day till ";
				break;
			case (Schedules.EVENT_WEEKLY):
				retString += "every week till ";
				break;
			case (Schedules.EVENT_MONTHLY):
				retString += "every month till ";
				break;
			}
			retString += event2.getRepetitionEnd() + ".";
			return retString;
		} else {
			return "";
		}
	}

	public String getShortTitle() {
		if (schEvent.getObject().getSchTitle().length() > 20) {
			return schEvent.getObject().getSchTitle().substring(0, 17) + "...";
		} else {
			return schEvent.getObject().getSchTitle();
		}
	}

	private Date currentDate = null;
	private IModel<Schedules> schEvent = null;
	private static final long serialVersionUID = 8399610728985228198L;
}
